package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.dto.WorkFlowDTO;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.pro.WorkFlow;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.ProductionOrderMapper;
import com.qidianit.mes.pro.mapper.WorkFlowMapper;
import com.qidianit.mes.pro.mapper.WorkFlowProcessMapper;
import com.qidianit.mes.pro.service.ProscheduleService;
import com.qidianit.mes.vo.ProductionOrderPageVo;
import com.qidianit.mes.vo.ProductionOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Package Path: com.qidianit.mes.pro.service.impl
 * Author: Alhz
 * Description:
 * Date: 2025/9/23 19:29
 */

@Service
public class ProscheduleServiceImpl implements ProscheduleService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;

    @Autowired
    private ProcessMapper processMapper;


   @Autowired
   private WorkFlowMapper workFlowMapper;

    @Override
    public Result selectAllProductionOrders(ProductionOrderPageVo productionOrderPageVo, int page, int pageSize){
        PageHelper.startPage(page,pageSize);
        List<ProductionOrderVo> productionOrderPageVoList = productionOrderMapper.selectAllProductionOrders(productionOrderPageVo);
        PageInfo<ProductionOrderVo> pageInfo = new PageInfo<>(productionOrderPageVoList);
        return Result.success(pageInfo);
    }


    /**
     * 根据id取对应的数据
     * @param id
     * @return
     */
    @Override
    public Result selectProductionOrderById(Long id) {
        System.out.println(productionOrderMapper.selectProductionOrderById(id));
        return Result.success(productionOrderMapper.selectProductionOrderById(id));
    }

    /**
     * 根据产品编码 查询 对应的工艺  以及工艺对应的流程
     * @param productCode
     * @return
     */
    @Override
    public Result selectWorkFlowProcess(String productCode) {
        List<WorkFlowDTO> workFlowDTOList = workFlowMapper.selectWorkflowByProductCode(productCode);

        workFlowDTOList.forEach(workFlowDTO -> {
            workFlowDTO.setProcessList(processMapper.selectWorkFlowProcessByProductCode(workFlowDTO.getId()));
        });


        return Result.success(workFlowDTOList);
    }

}
